<template>
  <van-icon
    :color="value === 1 ? 'red' : ''"
    :name="value === 1?'good-job':'good-job-o'"
    @click="onCollect"
    loading="loading"
  />
</template>

<script>
import { addLike, deleteLike } from '@/api/article'
export default {
  data () {
    return {

    }
  },

  props: {
    value: {
      type: Number,
      required: true
    },
    articleId: {
      type: [Number, String, Object],
      required: true
    }
  },

  created () {

  },

  methods: {
    async onCollect () {
      // 开始请求
      this.loading = true
      try {
        // 是否点赞
        let status = -1
        if (this.value === 1) {
          // 父组件 传递articleId
          await deleteLike(this.articleId)
        } else {
          await addLike(this.articleId)
          status = 1
        }
        // 更新视图
        this.$emit('input', status)
        this.$toast.success(status === 1 ? '点赞成功' : '取消点赞')
      } catch (err) {
        this.$toast('操作失败，请重试')
      }
      // 请求结束
      this.loading = false
    }
  }
}
</script>

<style scoped lang='less'>

</style>
